#include "startCommand.h"

void StartCommand::run() {
    ConfigReader::init("config.ini");
    ErrorLog::init(ConfigReader::getProperty("ErrorLog"));
    AccessLog::init(ConfigReader::getProperty("AccessLog"));
    bool connected = DB::getInstance()->connect(
        ConfigReader::getProperty("DBHost"),
        ConfigReader::getProperty("DBUser"),
        ConfigReader::getProperty("DBPass"),
        ConfigReader::getProperty("DBName")
    );
    if (!connected) {
        ErrorLog::append("Cannot connect to DataBase");
    }
    bool convertSuccess;
    int port = ConfigReader::getProperty("Port").toUInt(&convertSuccess, 10);
    if (convertSuccess) {
        Server* srv = new Server();
        if (!srv->start(port)) {
            ErrorLog::append("Cannot start listening socket", true);
        } else {
            EventsManager::show(QString("Server started at port ").append( QString::number(port) ));
        }
    } else {
        ErrorLog::append("Cannot get a correct port number from configuration file");
    }
}

